const router = require('koa-router')();
router.prefix('/zabbix/api');
const ResultVo = appRequire('vo/ResultVo.js');

let server_session = 'zbx_session=eyJzZXNzaW9uaWQiOiIzY2I5OGQ2MDhhN2Q3NWQ3YjgyOTEzMzU3NmQ5MmY5MCIsInNlcnZlckNoZWNrUmVzdWx0Ijp0cnVlLCJzZXJ2ZXJDaGVja1RpbWUiOjE2MTU1MzcyOTksInNpZ24iOiJLU1hwZ3hLTStkQUNIUWhzaDJ1Sm0zMllodm5NODkzaHZ1RWZ5bk1DNnJkZmwwTUFZbDBYNjFabVdkTWxsQTByU2NXVlNyY2tYdkZIWklcL2wwSkRcL1VncnRGUEFGaEFSOFVRQnJoRHplMmxjNjdzU1UyY3pUcXBLREkzZlp2XC9rNGVaU25Eb0hHc3FDeWh2QUtIbzFQQ1E9PSJ9';
let server_session_js = 'zbx_session=eyJzZXNzaW9uaWQiOiIzY2I5OGQ2MDhhN2Q3NWQ3YjgyOTEzMzU3NmQ5MmY5MCIsInNlcnZlckNoZWNrUmVzdWx0Ijp0cnVlLCJzZXJ2ZXJDaGVja1RpbWUiOjE2MTU1MzcyNzksInNpZ24iOiJLU1hwZ3hLTStkQUNIUWhzaDJ1Sm0zMllodm5NODkzaHZ1RWZ5bk1DNnJkZmwwTUFZbDBYNjFabVdkTWxsQTByU2NXVlNyY2tYdkZIWklcL2wwSkRcL1VncnRGUEFGaEFSOFVRQnJoRHplMmxjNjdzU1UyY3pUcXBLREkzZlp2XC9rNHVxM0Y1dGtRXC92WmRnN0ExSmp6SENBPT0ifQ%3D%3D';   
let server_ip = 'http://172.25.62.10';
    var superagent = require('superagent')
    ,sysLoginURL = `${server_ip}/index.php`
    ,sysInfoURL = `${server_ip}/zabbix.php?sid=ca8dad8e485b058b&action=widget.systeminfo.view`
    ,sysMapURL = `${server_ip}/zabbix.php?action=map.view&mapname=%E4%B8%9C%E6%9B%B2%E7%9F%BF%E7%BD%91%E7%BB%9C%E6%8B%93%E6%89%91`;


router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

router.get('/findOne', async (ctx, next) => {
  try{
    let config = {};
    config.server_session = server_session;
    config.server_session_js = server_session_js;
    config.server_ip = server_ip;
    ctx.body = ResultVo.success('success',config);
  }catch(err){
    //集中处理错误
    err.msg = "查找zabbix服务配置失败!";
    ctx.app.emit('error', err, ctx);
  }
})

router.post('/save', async (ctx, next) => {
  try{
    let config = ctx.request.body;
    server_session = config.server_session;
    server_session_js = config.server_session_js;
    server_ip = config.server_ip;
    sysLoginURL = `${server_ip}/index.php`;
    sysInfoURL = `${server_ip}/zabbix.php?sid=ca8dad8e485b058b&action=widget.systeminfo.view`;
    sysMapURL = `${server_ip}/zabbix.php?action=map.view&mapname=%E4%B8%9C%E6%9B%B2%E7%9F%BF%E7%BD%91%E7%BB%9C%E6%8B%93%E6%89%91`;
    ctx.body = ResultVo.success('success',config);
  }catch(err){
    //集中处理错误
    err.msg = "设置zabbix服务配置失败!";
    ctx.app.emit('error', err, ctx);
  }
})

router.post('/sysLogin', async (ctx, next) => {
  let p_login = ctx.request.body;
  console.log(p_login);
  let res = await superagent.post(sysLoginURL)
  .send(p_login)
  .set('Content-Type', 'application/x-www-form-urlencoded');
  //res.header[""]；
  ctx.body = JSON.stringify(res);
})

router.get('/sysInfo', async (ctx, next) => {
  let res = await superagent.post(sysInfoURL)
  .set('Content-Type', 'application/jsot')
  .set('Cookie',server_session)
  console.log(res.body);
  let table = res.body;
  let css = "<style>table{background-color:red}</style>";
  // ctx.body = css+table.body;
  await ctx.render('zabbix/static/sysInfo', {table:table.body});
})

router.get('/sysSvg', async (ctx, next) => {
  await ctx.render('svgMap', {})
})

router.get('/map', async (ctx, next) => {
  function getSysMapData(){
    return new Promise(function(resolve,reject){
      superagent.get(sysMapDataURL)
        .set('Cookie',server_session)
        .end((err, res) => {
          // Calling the end function will send the request
          if(err){
            reject(err);
          }
          resolve(res);
        });
    })
  }
  let res = await getSysMapData(); 
  console.log(res.text);
  let text = res.text;
  text = text.replace(/imgstore/,`${server_ip}/imgstore`);
  ctx.body = text;
})

router.get('/sysMap', async (ctx, next) => {
  function getSysMap(){
    return new Promise(function(resolve,reject){
      superagent.get(sysMapURL)
        .set('Cookie',server_session_js)
        .set('Content-Type', 'text/html')
        .end((err, res) => {
          // Calling the end function will send the request
          if(err){
            reject(err);
          }
          resolve(res);
        });
    })
  }
  let res = await getSysMap(); 
  console.log(res);
  let text = res.text;
  await ctx.render('zabbix/static/sysMap', {});
})

//imgstore.php
router.get('/imgstore.php', async (ctx, next) => {
  let res = await superagent.get(`${server_ip}/imgstore.php?`)  
  .query(ctx.query)
  //.set('Content-Type', 'application/javascript')
  .set('Content-Type', 'image/png')
  .set('Cookie',server_session_js);
  ctx.body = res.body;
  //ctx.body = res
})

//jsLoader.php
router.get('/jsLoader.php', async (ctx, next) => {
  let res = await superagent.get(`${server_ip}/jsLoader.php`)  //
  .query(ctx.query)
  //.send(ctx.request.body)
  //.set('Content-Type', 'application/javascript')
  .set('Cookie',server_session_js);
  ctx.body = res.body;
  //ctx.body = res
})
//map.php
router.get('/map.php', async (ctx, next) => {
  let res = await superagent.get(`${server_ip}/map.php`)  //
  .query(ctx.query)
  //.set('Content-Type', 'application/javascript')
  .set('Cookie',server_session_js);
  ctx.body = res.body;
  //ctx.body = res
})
//zabbix.php
router.all('/zabbix.php', async (ctx, next) => {
  let res = await superagent.get(`${server_ip}/zabbix.php`)  //
  .query(ctx.query)
  .send(ctx.request.body)
  //.set('Content-Type', 'application/javascript')
  .set('Cookie',server_session_js);
  ctx.body = res.body;
  //ctx.body = res
})
//jsrpc.php
router.all('/jsrpc.php', async (ctx, next) => {
  let res = await superagent.get(`${server_ip}/jsrpc.php`)  //
  .query(ctx.query)
  .send(ctx.request.body)
  //.set('Content-Type', 'application/javascript')
  .set('Cookie',server_session_js);
  ctx.body = res.body;
  //ctx.body = res
})


router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

module.exports = router
